const { BASE_URL, META, VERSION } = require('../config/config');
const Model = require('../model/Model');
const model = new Model();

/**
 * 服务类
 * @method buildCache() 生成缓存数据
 * @method getViewData() 返回公共视图数据
 * @method getIndexViewData() 返回首页的视图数据
 * @method getListViewDataForCategory() 返回分类文章列表页面的视图数据
 * @method getListViewDataForTag() 返回标签文章列表页面的视图数据
 * @method getListViewDataForNew() 返回最新文章列表的视图数据
 * @method getListViewDataForRecommand() 返回推荐文章列表的视图数据
 * @method getDetailViewData() 返回文章详情页面的视图数据
 */
class Service {

    /**
     * 生成缓存数据
     */
    async buildCache() {
        // 生成元数据文件
        await model.updateDb();
    }

    /**
     * 返回公共视图数据
     */
    getViewData(data = {}) {
        return Object.assign({
            base_url: BASE_URL,
            meta: META,
            version: VERSION,
            categories: model.getCategoryData(),
            tags: model.getTagData(),
            menus: model.getMenuData(),
        }, data);
    }

    /**
     * 返回首页的视图数据
     */
    async getIndexViewData(pageId, pageSize) {
        let { list, total, pageCount, pagination } = await model.pagingListForNew(pageId, pageSize, BASE_URL + '/index-{pageId}.html');
        let breadcrumb = model.getBreadcrumbHtml();
        return this.getViewData({ list, pagination, breadcrumb, pageCount, total });
    }

    /**
     * 返回分类文章列表页面的视图数据 
     */
    async getListViewDataForCategory(pageId, pageSize, category1, category2) {
        let urlTpl = BASE_URL + '/';
        if (category1) {
            urlTpl += category1 + '/';
        }
        if (category2) {
            urlTpl += category2 + '/';
        }
        urlTpl += 'index-{pageId}.html';
        let { list, pagination } = await model.pagingListForCategory(category1, category2, pageId, pageSize, urlTpl);
        let breadcrumb = model.getBreadcrumbHtml(category1, category2);
        return this.getViewData({ list, pagination, breadcrumb });
    }

    /**
     * 返回标签文章列表页面的视图数据 
     */
    async getListViewDataForTag(pageId, pageSize, tag) {
        let breadcrumb = model.getBreadcrumbHtml('', '', [{ text: tag, url: '/tag/' + tag + '.html' }]);
        let { list, pagination } = await model.pagingListForTag(tag, pageId, pageSize, BASE_URL + '/tag/' + tag + '-{pageId}.html');
        return this.getViewData({ list, pagination, breadcrumb });
    }

    /**
     * 返回最新文章列表的视图数据
     */
    async getListViewDataForNew(pageId, pageSize) {
        let breadcrumb = model.getBreadcrumbHtml('', '', [{ text: '最新', url: 'new.html' }]);
        let { list, pagination } = await model.pagingListForNew(pageId, pageSize, BASE_URL + '/new-{pageId}.html');
        return this.getViewData({ list, pagination, breadcrumb });
    }

    /**
     * 返回推荐文章列表的视图数据
     */
    async getListViewDataForRecommand(pageId, pageSize) {
        let breadcrumb = model.getBreadcrumbHtml('', '', [{ text: '推荐', url: 'recommand.html' }]);
        let { list, pagination } = await model.pagingListForRecommend(pageId, pageSize, BASE_URL + '/recommand-{pageId}.html');
        return this.getViewData({ list, pagination, breadcrumb });
    }

    /**
     * 返回文章详情页面的视图数据
     */
    async getDetailViewData(title, category1, category2) {
        let breadcrumb = model.getBreadcrumbHtml(category1, category2);
        let data = await model.getDetailData(title, category1, category2);
        return this.getViewData({ data, breadcrumb });
    }
}

module.exports = Service;